﻿// -----------------------------------------------------------------------
// <copyright file="VideoMask.cs" company="Abyssalsoft">
// TODO: Update copyright text.
// </copyright>
// -----------------------------------------------------------------------

namespace Gui.Player.Main
{
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    using Metal.Player.Main;

    /// <summary>
    /// Video mask
    /// </summary>
    public partial class VideoMask : Gui.BaseMask.BaseMask, IMainView
    {
        #region Members

        /// <summary> Represents the controller for this mask </summary>
        private MainController controller;

        /// <summary> Timer that show / hides the controls after some seconds </summary>
        private Timer fullScreenTimer;

        /// <summary> Seconds of idle </summary>
        private int counter = 0;

        /// <summary> Stores the last mouse position, used to show controls on mouse move </summary>
        private int lastMousePosition;

        #endregion

        #region Constructor

        /// <summary>
        /// Initializes a new instance of the VideoMask class.
        /// </summary>
        public VideoMask()
        {
            try
            {
                this.InitializeComponent();

                this.controller = new Metal.Player.Main.MainController(this);

                this.ApplyTheme();

                this.fullScreenTimer = new Timer();
                this.fullScreenTimer.Interval = 1000;
                this.fullScreenTimer.Tick += new EventHandler(this.FullScreenTimer_Tick);
                this.MouseMove += new MouseEventHandler(this.OnMouseMove);

                // Subscribe to on media load event (used to switch between audio / video style)
                Core.Player.Events.Instance.BeforeOpen += new EventHandler(OnBeforeOpenFile);
            }
            catch (Exception ex)
            {
                Metal.User.UserMessage.ShowError(ex);
            }
        }

        #endregion

        #region Properties
        /// <summary>
        /// Sets the position bar the maximum value
        /// </summary>
        public int TrackBarMaximumValue
        {
            set
            {
                if (GetControl("TrackBar") != null)
                {
                    (GetControl("TrackBar") as Gui.Controls.MetalSlider).Maximum = value;
                }
            }
        }

        /// <summary>
        /// Sets the position bar value
        /// </summary>
        public int TrackBarValue
        {
            set
            {
                if (GetControl("TrackBar") != null)
                {
                    (GetControl("TrackBar") as Gui.Controls.MetalSlider).Value = value;
                }
            }
        }

        /// <summary>
        /// Sets the volume value
        /// </summary>
        public int Volume
        {
            set
            {
                if (GetControl("Volume") != null)
                {
                    (GetControl("Volume") as Gui.Controls.MetalSlider).Value = value;
                }
            }
        }

        /// <summary>
        /// Sets a value indicating whether the controls of this form are shown
        /// </summary>
        public bool HideControlsFullScreen
        {
            set
            {
                this.ParentForm.Controls["TopPanel"].Visible = !value;
                this.ParentForm.Controls["BottomPanel"].Visible = !value;
                this.ParentForm.Controls["LeftPanel"].Visible = !value;
                this.ParentForm.Controls["RightPanel"].Visible = !value;
                this.ParentForm.Controls["Close"].Visible = !value;
                this.ParentForm.Controls["Minimize"].Visible = !value;

                // If the video is in a splitter, hide second panel... to be improved!
                if (this.ParentForm.Controls["Spliter"] != null)
                {
                    if (value == true)
                    {
                        (this.ParentForm.Controls["Spliter"] as Gui.Controls.MetalSplitContainer).Panel2Collapsed = true;
                    }
                    else
                    {
                        (this.ParentForm.Controls["Spliter"] as Gui.Controls.MetalSplitContainer).Panel2Collapsed = false;
                    }
                }

                if (value == true)
                {
                    this.fullScreenTimer.Start();
                    Gui.Presentation.FormsManager.RemoveOwnerForm();
                }
                else
                {
                    this.fullScreenTimer.Stop();
                    Gui.Presentation.FormsManager.SetOwnerForm(this.ParentForm);
                }
            }
        }

        #endregion

        #region Events

        /// <summary>
        /// Occurs when the user moves the mouse
        /// </summary>
        /// <param name="sender">Object sender</param>
        /// <param name="e">Event args</param>
        private void OnMouseMove(object sender, MouseEventArgs e)
        {
            if (MousePosition.X + MousePosition.Y != this.lastMousePosition)
            {
                this.counter = 0;
                this.fullScreenTimer.Start();
                Cursor.Show();
                if (this.Controls["ControlsPanel"] != null)
                {
                    this.Controls["ControlsPanel"].Visible = true;
                }
            }

            this.lastMousePosition = MousePosition.X + MousePosition.Y;
        }

        /// <summary>
        /// Show / hide controls
        /// </summary>
        /// <param name="sender">Object sender</param>
        /// <param name="e">Event args</param>
        private void FullScreenTimer_Tick(object sender, EventArgs e)
        {
            if (this.counter == 5)
            {
                Cursor.Hide();
                if (this.Controls["ControlsPanel"] != null)
                {
                    this.Controls["ControlsPanel"].Visible = false;
                }

                this.fullScreenTimer.Stop();
            }

            this.counter++;
        }

        #endregion

        /// <summary>
        /// Switch to video style if opened file is a video format
        /// </summary>
        /// <param name="sender">Object sender</param>
        /// <param name="e">Event args</param>
        private void OnBeforeOpenFile(object sender, EventArgs e)
        {
            try
            {
                if (this.Visible == false)
                {
                    return;
                }

                this.controller.SetCurrentView(this);
            }
            catch (Exception ex)
            {
                Metal.User.UserMessage.ShowError(ex);
            }
        }
    }
}
